<template>
	<view class="content">
		<view class="form">
			<view class="item f-c-c">
				<view class="iconLeft f-c-c">
					<image src="../../static/login/icon_phone.png" mode="widthFix"></image>
				</view>
				<input @blur="blur($event)" class="inputItem" type="number" v-model="form.mobile" placeholder="请输入手机号"
					placeholder-class="plaCla" />
			</view>
			<view class=" f-c-c">
				<view class="itemCode f-c-c">
					<view class="iconLeft f-c-c">
						<image src="../../static/login/icon_suo.png" mode="widthFix"></image>
					</view>
					<input class="inputItem" type="password" v-model="form.code" placeholder="请输入验证码"
						placeholder-class="plaCla" />
				</view>
				<u-toast ref="uToast"></u-toast>
				<u-verification-code :seconds="seconds" ref="uCode" @change="codeChange">
				</u-verification-code>
				<view @click="getCode" class="getCode cw f-c-c f25">{{tips}}</view>
			</view>
			<view class="item f-c-c">
				<view class="iconLeft f-c-c">
					<image src="../../static/login/icon_suo.png" mode="widthFix"></image>
				</view>
				<input class="inputItem" type="password" v-model="form.password" placeholder="请输入密码"
					placeholder-class="plaCla" />
			</view>
			<view class="item f-c-c">
				<view class="iconLeft f-c-c">
					<image src="../../static/login/icon_suo.png" mode="widthFix"></image>
				</view>
				<input class="inputItem" type="password" v-model="form.passwordAgain" placeholder="请再次输入密码"
					placeholder-class="plaCla" @blur="blurAgain($event)"/>
			</view>
		</view>

		<view @click="toRegister" class="btnLogin f-c-c">完成</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					mobile: "",
					code: "",
					password: "",
					passwordAgain: ""
				},
				tips: '',
				seconds: 60,
				flag:[false,false],
				code:''
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (this.$refs.uCode.canGetCode) {
					if (!this.$u.test.mobile(this.form.mobile)) {
						this.$u.toast('请输入正确的手机号！')
						return
					}
					const { msg, data, success } = await this.$u.api.verificationCode({
						phone: this.form.mobile
					})
					success ? this.code = data : this.$u.toast(data)
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			blur(e){
				if(!this.$u.test.mobile(e.target.value)){
					this.$u.toast('请输入正确的手机号！')
				}else{
					this.flag[0]=true
				}
			},
			blurAgain(e){
				console.log(this.form.password);
				console.log(e.target.value);
				console.log(e.target.value==this.form.password);
				if(e.target.value!=this.form.password || e.target.value.trim()==''){
					this.$u.toast('两次密码不一致！！')
				}else{
					this.flag[1]=true
				}
			},
			async toRegister() {
				if(!this.flag[0]){
					this.$u.toast('请输入正确的手机号！')
					return
				}
				if(!this.flag[1]){
					this.$u.toast('两次密码不一致！！')
					return;
				}
				console.log(this.form.code);
				if (this.form.code != this.code) {
					this.$u.toast('验证码错误！')
					return
				}
				const {success,msg}=await this.$u.api.forgetPwd({
					phone:this.form.mobile,
					pwd:this.form.password,
					rePwd:this.form.passwordAgain
				})
				success?this.$back(1):this.$u.toast(msg)
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		height: 100vh;
		background-color: $uni-color-primary;
		padding: 0 90rpx;
		overflow-y: hidden;
	}

	/* 表单 */
	.form {
		margin: 40rpx 0 40rpx 0;
	}

	.item:first-child {
		margin-top: 120rpx;
	}

	.item,
	.itemCode {
		border: 1rpx solid #FFFFFF;
		margin: 60rpx 0 0 0;
		border-radius: 10rpx;
	}

	.itemCode {
		// margin-right: 100rpx;
	}

	.itemCode .iconLeft {
		margin: 0 30rpx;
	}

	.itemCode .inputItem {
		padding: 20rpx 0rpx 20rpx 20rpx;
	}

	.inputItem {
		padding: 20rpx 100rpx 20rpx 20rpx;
		border-left: 1rpx solid #FFFFFF;
	}

	.iconLeft {
		margin: 0 20rpx;
	}

	.iconLeft image {
		width: 25rpx;
	}

	.getCode {
		margin-top: 30px;
		color: #FF8710;
		font-weight: 600;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		padding: 7rpx 0;
		width: 220rpx !important;
		height: 88rpx;
		margin-left: 10rpx;
	}

	.btnLogin {
		background-color: #FFFFFF;
		margin: 140rpx 0 60rpx 0;
		border-radius: 10rpx;
		height: 90rpx;
		color: $uni-color-primary;
	}

	.plaCla {
		color: #ffffff;
		font-Size: 30rpx;
	}
	
	input{
		color: #FFFFFF;
	}
</style>
